package cn.lanqiao.book.controller;

import cn.lanqiao.book.service.impl.BookShelfServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
/*
* 用于处理加入书架请求(ajax请求)
* 请求中应包含加入书架书籍的id
* 响应中：
* 成功则返回字符串“加入书架成功”
* 失败则返回字符串“加入书架失败”
* */
@WebServlet("/addBookToShelf")
public class AddBookToShelf extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取书籍的id
        int book_id= Integer.parseInt(req.getParameter("b_id"));
        HttpSession session=req.getSession();
        //获取当前用户名
        String u_username= (String) session.getAttribute("u_username");
        //如果用户未登录
        if(u_username==null){
            resp.getWriter().write("请先登录");
            return;
        }
        if(BookShelfServiceImpl.getInstance().addBookToShelf(book_id,u_username)){
            resp.getWriter().write("加入书架成功");
        }else {
            resp.getWriter().write("加入书架失败");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
